import traceback

import easygui as eg
import configparser
import ZiHanHyol as zihan
from basic.logger import logger
import sys

from basic.registry import register

# 校验授权
from basic.windowOperate import cWindow

register = register()
result = register.checkAuthored()
if result <= 0:
    eg.msgbox("clicker认证失败", 'clicker', '确定')
    sys.exit(0)

while 1:
    title = "clicker"
    msg = "您好,欢迎使用clicker,请从本地选择您要执行任务的配置文件~"
    wdname = '火影忍者Online'
    mode = ''
    var_max = 10
    # 选择任务模式
    choose_mode = eg.buttonbox(msg, title, choices=('配置模式', '匹配模式', '采集模式', '退出'))
    # 加载日志配置
    if choose_mode == '配置模式':
        # 打开windows文件选择窗口
        filename = eg.fileopenbox()
        #  实例化configParser对象
        config = configparser.ConfigParser()
        config.read(filename, 'utf-8')
        # -sections得到所有的section，并以列表的形式返回
        sections = config.sections()
        taskNumber = len(sections)
        if taskNumber > 0:
            for task in sections:
                if config.has_option(task, 'mode'):
                    mode = config.get(task, 'mode')
                if config.has_option(task, "max"):
                    var_max = config.getint(task, 'max')
                configuration = zihan.Configuration(task, config.getint(task, 'times'), config.getint(task, 'index')
                                                    , config.getfloat(task, 'rate'), mode, var_max)
                try:
                    game = zihan.GameAssist(wdname, configuration)
                    game.start()
                except:
                    logger.error(traceback.format_exc())
        else:
            if eg.ynbox("配置文件中无数据,是否接续", title):
                pass
            else:
                sys.exit(3)
        pass
    elif choose_mode == '匹配模式':
        logger.info("还没想好怎么做")
    elif choose_mode == '采集模式':
        logger.info("=======采集模式使用指引==========")
        logger.info("1、通过鼠标点击任意位置，将返回对应的鼠标坐标。快来试试吧~")
        cW = cWindow()
        cW.where_is_mouse()
    else:
        sys.exit(3)
